package com.masterclient.app.businessmodule.minemodule.point;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.point.adapter.PointAdapter;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointNotesContract;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointNotesContractPresenter;
import com.masterclient.app.businessmodule.minemodule.point.model.PointNotes;
import com.masterclient.app.businessmodule.minemodule.point.model.PointNotesData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/10/12 15:16
 * 功能说明：
 */

public class PointFragment extends FCBaseFragment<PointNotesContractPresenter> implements PointNotesContract.View{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    PointAdapter adapter;

    private List<PointNotes> list;
    private int total;
    View emptyView;

   int page = 1;

   String type = "1";

   boolean hasMore;


    public static PointFragment getInstance(String type){
        PointFragment timeLimitedBuyFragment = new PointFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        timeLimitedBuyFragment.setArguments(bundle);
        return timeLimitedBuyFragment;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_point;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        this.type = (String) getArguments().get("type");

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PointAdapter();
        recyclerView.setAdapter(adapter);

        get_data();

        adapter.bindToRecyclerView(recyclerView);
        emptyView = View.inflate(getContext(),R.layout.empty_cash,null);
        emptyView.setVisibility(View.GONE);
        TextView tvTip = (TextView) emptyView.findViewById(R.id.tv_tip);
        tvTip.setText("您还没有"+("1".equals(type)?"金币收入":"金币支出"));
        TextView tvDesc = (TextView) emptyView.findViewById(R.id.tv_desc);
        tvDesc.setText("未来的你会感谢现在正在奋斗的自己");
        adapter.setEmptyView(emptyView);


        swipeLayout.setOnRefreshListener(()->{
            refreshList();
        });

        adapter.setOnLoadMoreListener(()->{
            if(hasMore){
                get_data();
            }
        },recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }


    private void get_data() {
        mPresenter.getPointNotesData(type,"" + page , BusinessConfig.PAGE_SIZE);
    }

    public void refreshList(){
        if(list != null){
            list.clear();
            adapter.notifyDataSetChanged();
        }
        page = 1;
        get_data();
    }


    @Override
    public void showData(PointNotesData data) {

        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        hasMore = data.isSh_has_more();
        if(list == null){
            list = new ArrayList<>();
        }

        list.addAll(data.getSh_integral_list());

        adapter.setNewData(list);
        if(list.size()<=0){
            emptyView.setVisibility(View.VISIBLE);
        }
        if(!hasMore){
            adapter.setEnableLoadMore(false);
        } else {
            page++;
        }
    }

    @Override
    public void loadFail(String s) {
        ToastUtil.showToast(s);
        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        adapter.loadMoreFail();
//        emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void loadError(String s) {
        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        ToastUtil.showToast(s);
//        emptyView.setVisibility(View.VISIBLE);
    }

}
